<?php
namespace api;
use controller;
use model\addressModel;

class address extends controller{

    public function __construct($container)
    {
        parent::__construct($container);

        //todo 登录校验
        $res = [
            'status'=>'1',
            'description'=>'请先登录',
        ];

        if( empty($_SESSION['customer_id']) ){
            echo json_encode( $res );
            die();
        }
    }

    public function single(){
        $customer_id = $_SESSION['customer_id'];
        $address_id = $_GET['id'];



        $address = addressModel::single( $address_id, $customer_id );

        echo json_encode( $address );
    }

    public function get_list(){

//        $res = [
//            'status'=>'1',
//            'description'=>'请先登录',
//        ];
//        if( empty($_SESSION['customer_id']) ){
//            echo json_encode( $res );
//            die();
//        }
        $customer_id = $_SESSION['customer_id'];

        $list = addressModel::get_list( $customer_id );

        echo json_encode( $list );

    }

    public function edit(){
        $res = [
            'status'=>'0',
        ];
//        if( empty($_SESSION['customer_id']) ){
//            echo json_encode( $res );
//            die();
//        }
        $customer_id = $_SESSION['customer_id'];

        $data = $_POST;
        $data['customer_id'] = $customer_id;
        $id = !empty( $_GET['id'] ) ? $_GET['id'] : '';

        if( $id ){
            $ret = addressModel::edit( $id, $data );
            // todo bug
            $res['id'] = $ret;
        }else{
            $ret = addressModel::add( $data );
            // todo bug
            $res['id'] = $ret;

        }

        echo json_encode( $res );
    }

    public function remove(){

        $res = [
            'status'=>'1',
            'description'=>'请先登录',
        ];

        if( empty($_SESSION['customer_id']) ){
            echo json_encode( $res );
            die();
        }
        $customer_id = $_SESSION['customer_id'];



        if( !empty( $_GET['address_id'] ) ){
            $id = $_GET['address_id'];
            $ret = addressModel::remove( $id, $customer_id );
        }


        echo json_encode( $ret );
    }

    public function set_default(){
        $res = [
            'status'=>'1',
            'description'=>'请先登录',
        ];

        $address_id = $_GET['address_id'];
        //todo 取消其他地址的默认选项
        //todo 设置改地址为默认值
        if( empty($_SESSION['customer_id']) ){
            echo json_encode( $res );
            die();
        }
        $customer_id = $_SESSION['customer_id'];

        $ret = addressModel::setDefault( $address_id, $customer_id );
        if( $ret ) {
            $res['status'] = 0;
            $res['description'] = '设置成功';
        }

        echo json_encode( $res );
    }

    public function get_default(){
        $res = [
            'status'=>'1',
            'description'=>'请先登录',
        ];
        if( empty($_SESSION['customer_id']) ){
            echo json_encode( $res );
            die();
        }
        $customer_id = $_SESSION['customer_id'];
        $address = addressModel::getDefault( $customer_id );

        echo json_encode( $address );
    }
}